package com.caltco.cargo.ops.modules.sys.service.impl;

import com.caltco.cargo.ops.auth.bo.AdminUserDetails;
import com.caltco.cargo.ops.common.base.gen.BaseServiceImpl;
import com.caltco.cargo.ops.modules.sys.dao.UserLevelDao;
import com.caltco.cargo.ops.modules.sys.entity.SysUserEntity;
import com.caltco.cargo.ops.modules.sys.entity.UserLevel;
import com.caltco.cargo.ops.modules.sys.service.UserLevelService;
import com.caltco.cargo.ops.modules.sys.vo.LevelVO;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserLevelServiceImp extends BaseServiceImpl<UserLevelDao, UserLevel> implements UserLevelService {
    private final UserLevelDao userLevelDao;

    public UserLevelServiceImp(UserLevelDao userLevelDao) {
        this.userLevelDao = userLevelDao;
    }

    public Long getUserIdByToken() {
        return ((AdminUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUserEntity().getUserId();
    }

    @Override
    public List<LevelVO> getLevel() {
        List<LevelVO> learnBookVOS = userLevelDao.selectJoinList(LevelVO.class,
                new MPJLambdaWrapper<UserLevel>()
                        .selectAll(UserLevel.class)
                        .select(SysUserEntity::getUsername)
                        .leftJoin(SysUserEntity.class, SysUserEntity::getId, UserLevel::getUserId)
                        .orderByAsc(UserLevel::getPoint)
        );
        return learnBookVOS;
    }

    @Override
    public LevelVO getByOwn() {
        LevelVO learnBookVOS = userLevelDao.selectJoinOne(LevelVO.class,
                new MPJLambdaWrapper<UserLevel>()
                        .selectAll(UserLevel.class)
                        .select(SysUserEntity::getUsername)
                        .leftJoin(SysUserEntity.class, SysUserEntity::getId, UserLevel::getUserId)
                        .eq(SysUserEntity::getId, getUserIdByToken())
                        .orderByAsc(UserLevel::getPoint)
        );
        return learnBookVOS;
    }

}
